package com.itheima.bos.web.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BaseAction<E>  extends ActionSupport implements ModelDriven<E>{
	//模型驱动
	protected E model;
	
	@Override
	public E getModel() {
		return model;
	}
	
	public BaseAction(){
		Type type = this.getClass().getGenericSuperclass();
		ParameterizedType ptype = (ParameterizedType) type;
		Class modelClass = (Class) ptype.getActualTypeArguments()[0];
		try {
			model = (E) modelClass.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected Integer page;
	protected Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	public void pushPageDateToValueStack(Page<E> page){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		ServletActionContext.getContext().getValueStack().push(map);
	}
	
	
}
